class WorkCommentsController < ApplicationController
  before_filter :protect, :load_work
  
   def new
    @comment = WorkComment.new
    @title = "发表评论"
    respond_to do |format|
      format.html
      format.js
    end
  end
  
  def create
    content = params[:workComment][:content]
    content = content.gsub(/\[emot\]/,"<img src='/images/emotions/")
    content = content.gsub(/\[\/emot\]/,".gif'>")
    params[:workComment][:content] = content
    @comment = WorkComment.new(params[:workComment])
    @comment.user = @login_user
    @comment.work = @work
    respond_to do |format|
      if (@comment.save && @work.work_comments << @comment)
        format.html { redirect_to profile_for(@work.user) }
        format.js
      else 
        #format.html { redirect_to new_comment_url(@post.blog, @post) } 
        format.js { render :nothing => true } 
      end
    end
  end
  
  def destroy
    @comment = WorkComment.find(params[:id])
    if @comment.authorized?(@login_user)
      @comment.destroy
    else
      redirect_to user_url
      return
    end
    respond_to do |format|
      format.js
    end
  end
  
  private
  def load_work
    @work = Work.find(params[:work_id])
  end
end
